package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.content.Intent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ListView listview;
    private String[] titles = {"朱永恒", "周诚声", "赖生根", "唐世聪", "老宋"};
    private String[] contents = {"2020.11.16", "2020.11.15", "2020.11.14", "2020.11.13", "2020.11.12"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        listview = findViewById(R.id.listview);
        listview.setAdapter(new MyBaseAdapter());

    }

    private void initData() {


    }

    private void initEvent() {
//        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Toast.makeText(MainActivity.this, "我点击了条目（下标还是当前的位置）" + i, Toast.LENGTH_SHORT).show();
//            }
//        });   //监听每个条目的点击事件
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this, TwoActivity.class);
                startActivity(intent);
                MainActivity.this.finish();

            }
        });


    }

    class ViewHolder {
        TextView title, conent;
        ImageView iv;
    }

    class MyBaseAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return titles.length;   //返回长度
        }

        @Override
        public Object getItem(int i) {
            return titles[i];
        }

        @Override
        public long getItemId(int i) {
            return i;           //返回下标
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder = null;  //当前的视图是否存在

            if (viewHolder == null) {  //当视图不存在的时候我们需要创建
                //加载item布局
                view = View.inflate(MainActivity.this, R.layout.item_layout, null);
                //初始化ViewHoldr
                viewHolder = new ViewHolder();
                //通过viewHolder初始化控件
                viewHolder.title = view.findViewById(R.id.item_title_tv);
                //在listview条目布局里缓存控件
                view.setTag(viewHolder);
            } else {
                //如果已经在缓存中存在当前视图，则直接获取当前视图
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.title.setText(titles[i] + "");

            TextView content = view.findViewById(R.id.item_title_data);
            content.setText(contents[i] + "");
            ImageView view1 = findViewById(R.id.item_title_iv);
            return view;
        }
    }
}
